// open window
function openWindow(params) {

	if (top == null) {
        $.blockUI();
	} else {
		top.$.blockUI();
	}
    
	var _url = params.url ;
	var _winName = params.name ;
	var _width = params.width ;
    var _height = params.height ;
	var _left = params.left ;
    var _top = params.top ;
    
	//showProcess(true); //开启进度提示层

	if (!_winName) _winName = "";

	if (isNaN(parseInt(_width))) _width = 1024;//DEFAULT_DIALOG_WIDTH;
    if (isNaN(parseInt(_height))) _height = 740;//DEFAULT_DIALOG_HEIGHT;
    if (isNaN(parseInt(_left))) _left = (screen.availWidth-_width)/2;
    if (isNaN(parseInt(_top))) _top = (screen.availHeight-_height)/2;

    var params = null ;
    params  = "height=" + _height ;
    params += ",width=" + _width ;
    params += ",left=" + _left ;
    params += ",top=" + _top ;
    params += ",fullscreen=no,menubar=no,resizable=no" ;
    params += ",titlebar=no,toolbar=no,location=no,scrollbars=no,status=no" ;
    
	var _newWin = window.open(_url, _winName, params);
	
	//_newWin.resizeTo(_width,_height);
    //_newWin.moveTo(_left, _top);
    
    return _newWin;
}

function closeWindow() {
	
	$.unblockUI();
	window.close();
}

function unblock() {
    if (opener != null) {
        if (opener.top != null) {
        	opener.top.$.unblockUI();
        } else {
            opener.$.unblockUI();
        }
    }
}

// --- form function ----
var DEFAULT_WIN_WIDTH = 780 ;
var DEFAULT_WIN_HEIGHT = 800 ;
var DEFAULT_WIN_LEFT = 0 ;
var DEFAULT_WIN_TOP = 0 ;


/**
 * 提交页面表单并打开默认的全屏窗口显示结果
 * 参数： String，提交的url
 *       Object，Form对象或Form的名字
 *       String, 新窗口名字
 * 返回：Object, 新窗口对象
 *           
 * e.g. :  submitPage("http://localhost:9080/4GPOS/welcome.do", "LoginForm")
 *		   submitPage("http://localhost:9080/4GPOS/welcome.do", "LoginForm", "newWin")
 * 
 */
function submitPage(_url, _form, _winName) {
	return submitDialog(_url, _form, _winName, DEFAULT_WIN_WIDTH, DEFAULT_WIN_HEIGHT, DEFAULT_WIN_LEFT, DEFAULT_WIN_TOP);
}


/**
 * 提交页面表单到打开默认的Dialog窗口显示结果
 * 参数： String，提交的url
 *       Object，Form对象或Form的名字
 *       String, 新窗口名字
 *       int,    窗口宽度
 *       int，   窗口长度
 *       int，   窗口左部起始位置
 *       int，   窗口顶部起始位置
 * 返回：Object, 新窗口对象
 *           
 * e.g. :  submitDialog("http://localhost:9080/4GPOS/welcome.do", "LoginForm")
 *		   submitDialog("http://localhost:9080/4GPOS/welcome.do", "LoginForm", "newWin")
 * 
 */
function submitDialog(_url, _form, _winName, _width, _height, _left, _top) {
	if ("string" == typeof(_form)) {
		_form = document.getElementById(_form);
	}
	if (!_winName) _winName = _form.name + "_Window";

	var _tmpTarget = _form.target;
	var _tmpAction = _form.action;

	var _win = showDialog("", _winName, _width, _height, _left, _top);
	_form.target = _winName;
	_form.action = _url;
	_form.submit();
	
    _form.target = _tmpTarget;
	_form.action = _tmpAction;

	return _win;
}




/**
 * 显示进度提示并屏蔽键盘操作（一般用于页面提交时）
 * 参数：boolean，是否显示遮挡层
 *           
 * e.g. :  showProcess() // 显示提示层
 *         showProcess(true) // 显示遮挡层及提示层
 * 
 */
function showProcess(_showCover) {
	if (_showCover) {
		showElement(true, "coveringDIV");
	}
    showElement(true, "processDIV");
    
    //setKeyControl(null, true); // 页面键盘控制失效
}

/**
 * 恢复键盘操作隐藏进度提示
 * 参数：无
 *           
 * e.g. :  hideProcess() 
 * 
 */
function hideProcess() {
	//initKeyControl(); // 恢复页面键盘控制设置
	
	showElement(false, "processDIV");
    showElement(false, "coveringDIV");
}

// --- xml function ---- 
/**
 * 把XML字符串转为js对象
 *
 * @param xml String  xml格式的字符串
 * @return Object 一个对象
 * eg.
    var xml = "
	<result>
	  <exception text='just test'>
	    <code>e0001</code>
	    <message>exception test</message>
	  </exception>
	</result> 
    " ;
    结果为：
    var obj = xml2Object(xml) ;
    alert(obj.exception.text) ;//提示为：just test
    alert(obj.exception.message.innerText) ; //提示为：exception test ，注意innerText的用法。



    var xml = "
	<result>
	  <exception text='just test'>
	    <code>e0001</code>
	    <code><![CDATA[e0002]]></code>
	    <message>exception test</message>
	  </exception>
	</result> 
    " ;
    结果为：
    var obj = xml2Object(xml) ;
    alert(obj.exception.code[0].innerText) ;//提示为：e0001，注意，当同一层中出现相同名字的结点，会转为数组
    alert(obj.exception.code[1].innerText) ; //提示为：e0002，注意"<![CDATA[...]]>"。
 */
function xml2Object(xml){
    var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.loadXML(xml);
    var e=xmlDoc.documentElement;
  
    return elem2Object(e) ;
}

/**
 * 对xml 中的每一个元素转为对象，是一个递归算法
 *
 * @param elem xmlElement xml中的元素
 * @return Object 返回转化后的对象
 */
function elem2Object (elem) {
    var obj = null ;
  
    var elems = elem.childNodes ;

    if (elems.length == 0) { // 没有子结点，不用再递归
        obj = elem.text ;
    } else { 
        obj = new Object() ;
        for (var i = 0 ; i < elems.length ; i ++) {
            var index = elems[i].nodeName ;
            if (index.indexOf("#text") > -1 || index.indexOf("#cdata-section") > -1) {
                index = "innerText" ;
            }
            
            if (typeof(obj[index]) == "undefined") { // 不存在同名结点，新增
                obj[index] = elem2Object(elems[i]) ;
            } else { 
                if (typeof(obj[index][0]) == "undefined") { // 存在且非Array对象　
                    var temp = obj[index] ;
                    obj[index] = new Array() ;
                    obj[index].add(temp) ;
                }
                obj[index].add(elem2Object(elems[i])) ;
            }
            
        }
    
        var atts = elem.attributes ;
        for (var i = 0 ; i < atts.length ; i++) {
            obj[atts[i].name] = atts[i].value ;         
        }
    }
    return obj ;
}



// 把表单中的所有元素转为xml(name & value 对应所为节点的属性)
function form2Xml(formElem,formNameAlias){

    var node = null ;
    var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    
    var formName = null ;
    if (typeof(formNameAlias) == "undefined" && formNameAlias == null) {
        formName = formNameAlias ;
    } else {
        formName = formElem.name ;
    }
    
    xmlDoc.loadXML("<" + formName + "></" + formName + ">\n");
    var root=xmlDoc.documentElement;
    
    var elems = formElem.elements ;

    for( var i = 0 ; i < elems.length ; i ++ ) {
        var newNode = xmlDoc.createElement(elems[i].name)
        newNode.appendChild(xmlDoc.createCDATASection(elems[i].value)) ;
        root.appendChild(newNode) ;
        //root.setAttribute(elems[i].name,elems[i].value) ;
    }
    
    return xmlDoc.xml ;
}

// xml(name & value 对应所为节点的属性)转换为把表单中的元素
function xml2Form(xmlAsString,formElem){

    var node = null ;
    var xmlObj = xml2Object(xmlAsString) ;

    var elems = formElem.elements ;

    for( var i = 0 ; i < elems.length ; i ++ ) {

    	if (elems[i].type == "button") {
    		continue ;
    	}
    	
    	var elemName = elems[i].name ;
    	
    	var docIndex = elemName.indexOf(".") ;
    	if (docIndex > 0) {
    		elemName = elemName.substring(++docIndex) ;
    	}
    	
        var eValue = xmlObj[elemName] ;
     
        if (typeof(eValue) != "undefined" 
                && eValue != null
                && typeof(eValue.innerText) != "undefined") {
             eValue = eValue.innerText ;
        }
        
        if (typeof(eValue) == "undefined" || eValue == null) {
			elems[i].value = "" ;
		} else {
			elems[i].value = eValue ;
		}
        //root.setAttribute(elems[i].name,elems[i].value) ;
    }
}

function isUndefined(elem) {
	return (typeof(elem) == "undefined") ;
}
function isDefined(elem) {
	return (typeof(elem) != "undefined") ;
}

function e_(elementId) {
	return document.getElementById(elementId) ;
}
function v_(inputName, form) {

    if (typeof(form) == "undefined") form = document.forms[0] ;
    if (typeof(form) == "string") form = e_(form) ;
    
    var inputObjs = form[inputName] ;
    
    if (typeof(inputObjs.length) == "undefined") {
        inputObjs[0] = inputObjs ; 
    } 
    
    var result = null ;
    
    var inputObj = null ;
    for (var i = 0 ; i < inputObjs.length ; i ++) {
        inputObj = inputObjs[i] ;
	    if (inputObj.type == "radio") {
	        if (inputObj.checked) {
	            return inputObj.value ;
	        }
	    } else if (inputObj.type == "checkbox") {
	        if (inputObj.checked) {
		        if (result == null) {
		            result = [] ;
		        }
		        result[result.length] = inputObj.value ;
		    }
	    } else {
	        return inputObj.value ;
	    }
    }
    
    return result ;
}

function openWindow2(windowName, windowTitle, url, params, buttonCalls, windowWidth, windowHeight) {

	setWindowParams(windowName, params) ;
	
	var iframe = '<iframe src="' + url + '" name="' + windowName + '" id="' + windowName + '" frameborder="0" width="100%" height="100%"></iframe>' ;
    var divFrame = '<div style="width:100%;heigth:100%;">' + iframe + '</div>' ; 

    if (typeof(windowWidth) == "undefined") {
    	windowWidth = 910 ;
    }
    
    if (typeof(windowHeight) == "undefined") {
    	windowHeight = 760 ;
    }
    
	var windowMe = top.$(divFrame).dialog({
	    title: windowTitle,
	    autoOpen: true,
	    width: windowWidth,
	    height: windowHeight,
	    modal: true,
	    resizable: true,
	    autoResize: true,
	    overlay: {
	        opacity: 0.5,
	        background: "black"
	    },
	    resizable:false,
        buttons: buttonCalls
	});
	
	params.windowMe = windowMe ;
	
	return windowMe ;
}

function getWindowParams(windowName) {
	if (typeof(top.windowParams) == "undefined") {
		top.windowParams = [] ;
	}
	
	var params = top.windowParams[windowName] ;
	
	if (params == null || typeof(params) == "undefined") {
		return params ;
	} else {
		top.windowParams[windowName] = null ;
		return params ;
	}
}

function setWindowParams(windowName, params) {
	if (typeof(top.windowParams) == "undefined") {
		top.windowParams = [] ;
	}

	top.windowParams[windowName] = params ;
}